<?php
// $Id: pastelog.controller.php,v 1.8 2006/03/03 13:49:53 mutante_power_b Exp $

require_once "pastelog.model.php";

class PasteLogController extends MVC_Controller
{
    function PasteLogController() {
        parent::MVC_Controller();

        $this->view->template->assign('pastelog_version', '1.0-RC2');

        $this->view->template->register_modifier('htmlentities', 'htmlentities');
        $this->view->template->register_modifier('sprintf', 'sprintf');
        $this->view->template->register_modifier('sslash', 'stripslashes');
        $this->view->template->register_modifier('var_dump', 'var_dump');

        $this->view->template->register_function('url', 'my_smarty_url');
        $this->view->template->config_load(LANGUAGE.".conf");

        $this->model = new PasteLogModel();
        $this->model->autoManagement();
    }

    function defaultMethod()
    {
        $this->displayNew();
    }

    function _assignGeSHiLangs($selected = 'php')
    {
        $langs = array(
            'actionscript' => 'ActionScript',
            'actionscript-french' => '&nbsp;&nbsp;ActionScript (French Doc Links)',
            'ada' => 'Ada',
            'apache' => 'Apache Log File',
            'applescript' => 'AppleScript',
            'asm' => 'ASM (NASM based)',
            'asp' => 'ASP',
            'bash' => 'Bash',
            'blitzbasic' => 'BlitzBasic',
            'c' => 'C',
            'c_mac' => 'C for Macs',
            'caddcl' => 'CAD DCL',
            'cadlisp' => 'CAD Lisp',
            'cpp' => 'C++',
            'csharp' => 'C#',
            'css' => 'CSS',
            'd' => 'D',
            'delphi' => 'Delphi',
            'diff' => 'Diff',
            'div' => 'DIV',
            'dos' => 'DOS',
            'eiffel' => 'Eiffel',
            'freebasic' => 'FreeBasic',
            'gml' => 'GML',
            'html4strict' => 'HTML (4.0.1)',
            'inno' => 'Inno',
            'java' => 'Java',
            'javascript' => 'Javascript',
            'lisp' => 'Lisp',
            'lua' => 'Lua',
            'matlab' => 'Matlab',
            'mpasm' => 'MPASM',
            'mysql' => 'MySQL',
            'nsis' => 'NullSoft Installer',
            'objc' => 'Objective C',
            'ocaml' => 'OCaml',
            'ocaml-brief' => '&nbsp;&nbsp;OCaml (Brief)',
            'oobas' => 'Openoffice.org BASIC',
            'oracle8' => 'Oracle 8',
            'pascal' => 'Pascal',
            'perl' => 'Perl',
            'php' => 'PHP',
            'php-brief' => '&nbsp;&nbsp;PHP (Brief version)',
            'text' => 'Plain Text',
            'python' => 'Python',
            'qbasic' => 'QBasic/QuickBASIC',
            'ruby' => 'Ruby',
            'scheme' => 'Scheme',
            'sdlbasic' => 'SDLBasic',
            'smarty' => 'Smarty',
            'sql' => 'SQL',
            'vb' => 'VisualBasic',
            'vbnet' => 'VB.NET',
            'vhdl' => 'VHDL',
            'visualfoxpro' => 'VisualFoxPro',
            'xml' => 'XML',
        );

        $this->view->assign('geshi_selected', $selected);
        $this->view->assign('geshi_langs', $langs);
    }

    function _antiFlood()
    {
        $longip = ip2long($_SERVER['REMOTE_ADDR']);

        $total = $this->model->countPastesByIP($longip);

        return ($total >= 1);
    }

    function _displayBase()
    {
        $recents = $this->model->getRecents();
        $this->view->assign('recents', $recents);
            
        if (isset($_COOKIE['nickname']) && !empty($_COOKIE['nickname'])) {
            $this->view->assign('nickname', $_COOKIE['nickname']);
        }

        $this->view->assign('show_captcha', $this->_antiFlood());
    }
    
    function displayNew()
    {
        $this->_displayBase();
        $this->_assignGeSHiLangs();
        $this->view->display('new');
    }

    function displayPaste($id)
    {
        $paste = $this->model->getPaste($id);

        if (is_null($paste)) {
            $this->displayNew();
            return;
        }

        $paste['nickname'] = mb_convert_encoding($paste['nickname'], 'HTML-ENTITIES', 'auto');

        $geshi = new GeSHi($paste['code'], $paste['lang']);

        $geshi->set_header_type(GESHI_HEADER_DIV);
        
        if (!isset($_GET['ln']) || $_GET['ln']) {
            $this->view->assign('pastelog_ln', true);
            $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, 2);
        } else {
            $this->view->assign('pastelog_ln', false);
            $geshi->enable_line_numbers(GESHI_NO_LINE_NUMBERS, 2);
        }
        
        $geshi->set_line_style('background: #eeeeee;', 'background: #ffffff;');

        $geshi->set_tab_width(4);

        $paste['parsed'] = $geshi->parse_code();
        $paste['code'] = htmlspecialchars($paste['code']);

        $paste['link'] = url($id);
        
        $this->_displayBase();
        $this->_assignGeSHiLangs($paste['lang']);
        $this->view->assign('paste', $paste);
        $this->view->display('view');
    }

    function savePaste()
    {
        extract($_POST);

        if (isset($captcha)) {
            require_once 'libs/php-captcha.inc.php';

            if (!PhpCaptcha::Validate($captcha)) {
                die('C&oacute;digo de prote&ccedil;&atilde;o Invalido!');
            }
        }
        
        $nickname = trim($nickname);
        $nickname = empty($nickname) ? 'An&ocirc;nimo' : $nickname;
        
        $t_code = trim($code);
        if (empty($t_code)) {
            header('location: new');
            exit;
        }
        
        $code = stripslashes($code);
        
        if (isset($cookie)) {
            setcookie('nickname', $nickname, strtotime('+90 days'));
        }

        $longip = ip2long($_SERVER['REMOTE_ADDR']);

        $data = array($nickname, $lang, $code, $longip);
        
        $id = $this->model->savePaste($data);
        header('location: '.url($id));
        exit;
    }

    function downloadPaste($id)
    {
        $paste = $this->model->getPaste($id);
        $file = "$id.txt";
        
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Content-Type: application/force-download");
        header( "Content-Disposition: attachment; filename=$file;" );
        header("Content-Transfer-Encoding: binary");
        header("Content-Length: ".strlen($paste['code']));

        print $paste['code'];
        exit;
    }

    function displayAbout()
    {
        $this->_displayBase();
        $this->view->display('about');
    }

    function displayCaptcha()
    {
        require_once "libs/php-captcha.inc.php";

        $aFonts = array('libs/VeraBd.ttf', 'libs/VeraIt.ttf', 'libs/Vera.ttf');
        $oPhpCaptcha = new PhpCaptcha($aFonts, 150, 40);
        $oPhpCaptcha->UseColour(true);
        $oPhpCaptcha->SetNumChars(4);
        $oPhpCaptcha->SetNumLines(35);
        $oPhpCaptcha->Create();
        exit;
    }
}

?>
